package com.sri.controller.search;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sri.business.search.SearchManager;
import com.sri.cache.CacheManager;
import com.sri.formBeans.OptSearchForm;
import com.sri.formBeans.SearchForm;
import com.sri.lucene.search.HitPageCollector;
import com.sri.model.home.Home;

/**
 * @author skodali
 * 
 */
public class SearchHomesAction extends DispatchAction {
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	/*
	 * public ActionForward execute(ActionMapping mapping, ActionForm form,
	 * HttpServletRequest request, HttpServletResponse response) { return
	 * super.execute(mapping, form, request, response); }
	 */

	/**
	 * Method search
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward searchHomes(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SearchForm searchForm = (SearchForm) form;
		// search the Homes based upon the users input
		request.setAttribute("homes", SearchManager.searchHomes(searchForm));
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(2));
		return mapping.findForward("success");
	}

	/**
	 * Method getHomes - Optimized Lucene search
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getHomes(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// Optimized Lucene search - searches the Homes based upon the users
		// input
		String searchWord = request.getParameter("searchKey");
		if (request.getParameter("nav") != null)
			pagination(request);
		else {
			initialize();
		}

		try {
			Map<String, Object> map = HitPageCollector.search(searchWord.toLowerCase(),
					pageStart, pageMax);
			request.setAttribute("homes", (List<Home>) map.get("list"));
			if ((Integer) map.get("total") > pageMax)
				request.setAttribute("forward", "true");
			if (pageStart > pageMax)
				request.setAttribute("backward", "true");
			if (searchWord != null)
				request.setAttribute("searchForm", (OptSearchForm) form);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mapping.findForward("success");
	}

	private void initialize() {
		pageStart = 1;
		pageMax = 50;
	}

	private void pagination(HttpServletRequest req) {
		if (req.getParameter("nav") != null
				&& req.getParameter("nav").equals("more"))
			pageStart = pageStart + pageMax;
		else if (req.getParameter("nav") != null
				&& req.getParameter("nav").equals("less")
				&& pageStart > pageMax)
			pageStart = pageStart - pageMax;

	}

	/**
	 * Method getCities by stateId
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getCities(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int stateId = Integer.parseInt(request.getParameter("stateId"));
		request.setAttribute("stateCities", CacheManager
				.getCitiesByStateId(stateId));
		return mapping.findForward("success");
	}

	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// search the Homes based upon the users input
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(2));
		return mapping.findForward("success");
	}

	int pageStart = 1;
	int pageMax = 50;

}
